package com.sskj.level.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.level.R;
import com.sskj.level.R2;
import com.sskj.level.bean.BuySellEnum;
import com.sskj.level.bean.PingCangType;
import com.sskj.level.bean.TradeListBean;
import com.sskj.level.presenter.DealFragmentPresenter;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.mvchelper.RefreshDataSource;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;
import io.reactivex.Flowable;

@Route(path = RConfig.LEVEL_FRAGMENT_DEAL)//成交
public class DealFragment extends BaseFragment<DealFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private SlimAdapter<TradeListBean> slimAdapter;
    private MVCCoolHelper<List<TradeListBean>> mvcCoolHelper;


    @Override

    protected int getLayoutId() {
        return R.layout.level_fragment_deal;
    }

    @Override
    public DealFragmentPresenter getPresenter() {
        return new DealFragmentPresenter();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden && mvcCoolHelper != null) {
            mvcCoolHelper.refresh();
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public void initView() {

        HashMap<String, String> pcMap = new HashMap<>();
        pcMap.put("1", App.INSTANCE.getString(R.string.levelJavaDealFragment2));
        pcMap.put("2", App.INSTANCE.getString(R.string.levelJavaDealFragment4));
        pcMap.put("3", App.INSTANCE.getString(R.string.levelJavaDealFragment6));
        pcMap.put("4", App.INSTANCE.getString(R.string.levelJavaDealFragment8));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        slimAdapter = SlimAdapter.create().register(R.layout.level_recy_item_deal, new SlimInjector<TradeListBean>() {
            @Override
            public void onInject(TradeListBean data, IViewInjector injector, List payloads) {
                injector
                        .textColor(R.id.tv_mm, ContextCompat.getColor(App.INSTANCE, data.getOtype().equals(BuySellEnum.BUY.getType()) ? BuySellEnum.BUY.getColorRes() : BuySellEnum.SELL.getColorRes()))
                        .text(R.id.tv_mm, data.getOtype().equals(BuySellEnum.BUY.getType()) ? BuySellEnum.BUY.getTitle() : BuySellEnum.SELL.getTitle())//买入卖出
                        .text(R.id.tv_bz, data.getPname())//币种类型
                        .text(R.id.tv_time, data.getSelltime())//时间
                        .text(R.id.tv_first_left, data.getMarketLimit())//类型
                        .text(R.id.level_fd_type, App.INSTANCE.getString(R.string.levelJavaDealFragment9) + data.getProfit())//盈亏
                        .text(R.id.tv_first_right, data.getLeverage())//杠杆倍数
                        .text(R.id.tv_second_left, data.getBuyprice())//建仓价
                        .text(R.id.tv_second_right, data.getSellprice())//平仓价
                        .text(R.id.tv_third_left, data.getBuynum())//成交量
                        .text(R.id.tv_third_right, data.getTotalprice())//保证金
                        .text(R.id.tv_four_left, data.getSxfee())//手续费
                        .text(R.id.tv_four_right, data.getDayfee())//过夜费
                        .text(R.id.tv_five_left, data.getPoit_win())//止盈价
                        .text(R.id.tv_five_right, data.getPoit_loss())//止损价
                        .text(R.id.tvPingCang, pcMap.get(data.getPc_type()))//
                        .background(R.id.level_fd_type, ContextCompat.getDrawable(App.INSTANCE, Double.valueOf(data.getProfit()) > 0 ? R.drawable.level_shape_green2 : R.drawable.level_shape_red2))
                ;
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        ModelRx2DataSource<TradeListBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<TradeListBean>)
                page -> mPresenter.getTradeList(page + "", "10"), 10);

        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
    }
}
